package com.mk.util;

import me.hao0.wechat.core.Wechat;
import me.hao0.wechat.model.base.AuthAccessToken;
import me.hao0.wechat.model.js.Config;
import me.hao0.wechat.model.message.send.*;
import me.hao0.wechat.model.user.User;
import me.hao0.wechat.model.user.UserInfo;
import org.osgl.util.S;

import java.util.*;

@SuppressWarnings("all")
public class WechatUtils {
    public static Wechat wechat;

    //单发消息（openid,文本内容 ）
    public static void sendSingleText(String openid, String text) {
        //预览消息
        SendPreviewMessage msg = new SendPreviewMessage();
        msg.setOpenId(openid);
        msg.setType(SendMessageType.TEXT);
        msg.setContent(text);
        wechat.msg().previewSend(wechat.base().accessToken().getAccessToken(), msg);
    }

    /**
     * 获取用户openId
     *
     * @param code 用户授权的code
     * @return 用户的openId，或抛WechatException
     * @see #authAccessToken(String)
     */
    public static String getOpenId(String code) {
        return wechat.base().openId(code);
    }

    public static String getUnionId(String code) {
        return wechat.base().openId(code);
    }

    public static String authUrl(String redirectUrl) {
        return wechat.base().authUrl(redirectUrl, false);
    }

    public static AuthAccessToken authAccessToken(String code) {
        return wechat.base().authAccessToken(code);
    }

    public static UserInfo getUserInfo(AuthAccessToken authAccessToken) {
        return wechat.user().getUserInfo(authAccessToken);
    }

    public static User getUserInfo(String openid) {
        return wechat.user().getUser(openid);
    }

    public static UserInfo getUserInfoByCode(String code) {
        return wechat.user().getUserInfo(wechat.base().authAccessToken(code));
    }

    //分组消息sss
    public static void sendGroupText(Integer groupid, String text) {
        //预览消息
        SendMessage msg = new SendMessage();
        msg.setScope(SendMessageScope.GROUP);
        msg.setType(SendMessageType.TEXT);
        msg.setContent(text);
        msg.setGroupId(groupid);
        wechat.msg().send(wechat.base().accessToken().getAccessToken(), msg);
    }

    //分组消息
    public static void sendOpenidsText(List<String> list, String text) {
        //预览消息
        SendMessage msg = new SendMessage();
        msg.setScope(SendMessageScope.OPEN_ID);
        msg.setType(SendMessageType.TEXT);
        msg.setContent(text);
        msg.setOpenIds(list);
        wechat.msg().send(wechat.base().accessToken().getAccessToken(), msg);
    }

    //模板消息
    public static void sendTemplete(String openid, String tmpid, String link, Map<String, String> map) {
        if (S.isNotBlank(openid) && S.isNotBlank(tmpid)) {
            List<TemplateField> list = new ArrayList<>();
            for (String key : map.keySet()) {
                list.add(new TemplateField(key, MapUtil.getStr(map, key)));
            }
            wechat.msg().sendTemplate(wechat.base().accessToken().getAccessToken(), openid, tmpid, link, list);
        }
    }

    //获取JSSDK签名
    public static Map<String, Object> getJSSDKConfig(String url) {
        Map<String, Object> map = new HashMap<>();
        Config config = wechat.js().getConfig(new Date().getTime() + "", url);
        map.put("appId", config.getAppId());
        map.put("timestamp", config.getTimestamp());
        map.put("nonceStr", config.getNonStr());
        map.put("signature", config.getSignature());
        return map;
    }
}
